import pyodbc


def execute_query(query_string):
    # 设置数据库连接参数
    conn_params = {
        'Driver': '{MySQL ODBC 8.0 Unicode Driver}',
        'Server': 'localhost',
        'Database': 'world',
        'UID': 'root',
        'PWD': '123456'
    }

    # 创建连接对象
    conn = pyodbc.connect(
        ';'.join([f"{key}={value}" for key, value in conn_params.items()]))

    # 创建游标对象
    cursor = conn.cursor()

    # 执行查询
    cursor.execute(query_string)

    # 获取查询结果的列名
    columns = [column[0] for column in cursor.description]

    # 获取查询结果的所有行数据
    rows = cursor.fetchall()

    # 将查询结果转换成列表，每行数据作为一个字典
    result_set = [dict(zip(columns, row)) for row in rows]

    # 关闭游标和连接
    cursor.close()
    conn.close()

    # 返回查询结果
    return result_set


# result_set = execute_query("select code, name, continent from country")
# for r in result_set:
#     print(r)

result_set = execute_query("select * from cn_city")
for r in result_set:
    print(r)
